<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarDealer;

use AuctionCore\Account\Back\Employee;
use AuctionCore\Other\Address;
use AuctionCore\AntNest\UserCenter;

/**
 * 车商企业认证
 *
 * @author Xmh
 */
class DealerCompanyCertify
{
    private $CarDealer;
    private $CarDealerCertifyTraceLog;
    private $CarDealerCompanySignLog;
    private $Addr;

    public function __construct()
    {
        $this->CarDealer = M('car_dealer');
        $this->CarDealerCertifyTraceLog = M('car_dealer_certify_trace_log');
        $this->CarDealerCompanySignLog = M('car_dealer_sign_log');
        $this->Addr = new Address();
    }

    /**
     * 获取车商企业签约认证列表
     * @param $page
     * @param $limit
     * @param $params
     * @return array
     */
    public function getCompanyCertifyList($page, $limit, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        //搜索
        $map = array();
        $map['cd.auth_type'] = ['eq', 2];
        if ($params['status'] == 1) { //认证审核
            $map['cd.auth_status'] = ['eq', 1];
        } elseif ($params['status'] == 2) {  //待签约
            $map['cd.auth_status'] = ['eq', 2];
            $map['cd.is_agree_agreement'] = ['eq', 0];
            $map['cd.sign_trace_status'] = ['eq', 0];
        } elseif ($params['status'] == 3) {  //签约跟踪
            $map['cd.auth_status'] = ['eq', 2];
            $map['cd.is_agree_agreement'] = ['eq', 0];
            $map['cd.sign_trace_status'] = ['eq', 1];
            $map['cdsl.sign_result'] = ['eq', 1];
        } elseif ($params['status'] == 4) {  //跟踪签约成功
            $map['cd.auth_status'] = ['eq', 2];
            $map['cd.is_agree_agreement'] = ['eq', 1];
            $map['cd.sign_trace_status'] = ['eq', 3];
            $map['cdsl.sign_result'] = ['eq', 3];
        } elseif ($params['status'] == 5) {  //跟踪签约失败
            $map['cd.auth_status'] = ['eq', 2];
            $map['cd.is_agree_agreement'] = ['eq', 0];
            $map['cd.sign_trace_status'] = ['eq', 2];
            $map['cdsl.sign_result'] = ['eq', 2];
        } elseif ($params['status'] == 6) {  //认证失败
            $map['cd.auth_status'] = ['eq', 3];
        }
        if ($params['dealer_id'] > 0) {
            $map['cd.dealer_id'] = intval($params['dealer_id']);
        }
        if ($params['company_name']) {
            $map['cd.fullname'] = ['LIKE', '%' . $params['company_name'] . '%'];
        }
        if ($params['business_license_num']) {
            $map['cd.business_license_num'] = ['LIKE', '%' . $params['business_license_num'] . '%'];
        }
        if ($params['auth_name']) {
            $map['cd.contact_person'] = ['LIKE', '%' . $params['auth_name'] . '%'];
        }
        if ($params['auth_phone']) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile($params['auth_phone'], 2, 1);
            $map['du.uid'] = ['eq', $info1['account_id']];
            //$map['cd.contact_mobile'] = ['LIKE', '%' . $params['auth_phone'] . '%'];
        }
        //客户经理
        if ($params['manager_id'] > 0) {
            $map['dm.emp_id'] = intval($params['manager_id']);
        }
        if ($params['start_time']) {
            if ($params['status'] == 1) {
                $map['cd.apply_auth_time'] = ['egt', $params['start_time'] . ' 00:00:00'];
            } elseif ($params['status'] == 2 || $params['status'] == 6) {
                $map['cd.auth_audit_time'] = ['egt', $params['start_time'] . ' 00:00:00'];
            } elseif ($params['status'] == 3) {
                $map['cdsl.next_trace_time'] = ['egt', $params['start_time'] . ' 00:00:00'];
            } elseif ($params['status'] == 4) {
                $map['cd.sign_time'] = ['egt', $params['start_time'] . ' 00:00:00'];
            } elseif ($params['status'] == 5) {
                $map['cdsl.updatetime'] = ['egt', $params['start_time'] . ' 00:00:00'];
            }
        }
        if ($params['end_time']) {
            if ($params['status'] == 1) {
                $map['cd.apply_auth_time'] = ['egt', $params['end_time'] . ' 23:59:59'];
            } elseif ($params['status'] == 2 || $params['status'] == 6) {
                $map['cd.auth_audit_time'] = ['egt', $params['end_time'] . ' 23:59:59'];
            } elseif ($params['status'] == 3) {
                $map['cdsl.next_trace_time'] = ['egt', $params['end_time'] . ' 23:59:59'];
            } elseif ($params['status'] == 4) {
                $map['cd.sign_time'] = ['egt', $params['end_time'] . ' 23:59:59'];
            } elseif ($params['status'] == 5) {
                $map['cdsl.updatetime'] = ['egt', $params['end_time'] . ' 23:59:59'];
            }
        }
        if ($params['start_time'] && $params['end_time']) {
            if ($params['status'] == 1) {
                $map['cd.apply_auth_time'] = [['egt', $params['start_time'] . ' 00:00:00'], ['elt', $params['end_time'] . ' 23:59:59']];
            } elseif ($params['status'] == 2 || $params['status'] == 6) {
                $map['cd.auth_audit_time'] = [['egt', $params['start_time'] . ' 00:00:00'], ['elt', $params['end_time'] . ' 23:59:59']];
            } elseif ($params['status'] == 3) {
                $map['cdsl.next_trace_time'] = [['egt', $params['start_time'] . ' 00:00:00'], ['elt', $params['end_time'] . ' 23:59:59']];
            } elseif ($params['status'] == 4) {
                $map['cd.sign_time'] = [['egt', $params['start_time'] . ' 00:00:00'], ['elt', $params['end_time'] . ' 23:59:59']];
            } elseif ($params['status'] == 5) {
                $map['cdsl.updatetime'] = [['egt', $params['start_time'] . ' 00:00:00'], ['elt', $params['end_time'] . ' 23:59:59']];
            }
        }
        $cols = 'cd.dealer_id,cd.fullname,cd.business_license_num,cd.contact_person,
                 cd.auth_type,cd.apply_auth_time,cd.auth_audit_time,cd.sign_time,du.uid,
                 cdsl.next_trace_time,cdsl.sign_failed_remark,cdsl.updatetime as sign_failed_time';
        $prefix = C('DB_PREFIX');
        if ($params['status'] == 1) {
            $order = 'cd.apply_auth_time DESC';
        } elseif ($params['status'] == 2 || $params['status'] == 6) {
            $order = 'cd.auth_audit_time DESC';
        } elseif ($params['status'] == 3) {
            $order = 'cdsl.next_trace_time DESC';
        } elseif ($params['status'] == 4) {
            $order = 'cd.sign_time DESC';
        } elseif ($params['status'] == 5) {
            $order = 'cdsl.updatetime DESC';
        } else {
            $order = 'cd.dealer_id DESC';
        }
        $total = $this->CarDealer->alias('cd')
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "car_dealer_sign_log as cdsl ON cdsl.dealer_id = cd.dealer_id AND sign_type = 2")
            ->where($map)->count();
        $list = $this->CarDealer->alias('cd')->field($cols)
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "car_dealer_sign_log as cdsl ON cdsl.dealer_id = cd.dealer_id AND sign_type = 2")
            ->where($map)
            ->limit($limit)
            ->page($page)
            ->order($order)
            ->select();
        if (!empty($list)) {
            $dealer = new Dealer();
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
//        echo M()->_sql();
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                if ($val['business_city']) {
                    $list[$key]['business_city_name'] = $this->Addr->get_name_by_code($val['business_city']);
                } else {
                    $list[$key]['business_city_name'] = '未指定';
                }
                $list[$key]['auth_type'] = C('DEALER_CERTIFY_TYPE')[$val['auth_type']];
                $list[$key]['next_trace_time'] = $val['next_trace_time'] ? $val['next_trace_time'] : '--';
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取车商个人签约详情
     * @param $dealer_id
     * @return bool|mixed
     */
    public function getCompanyCertifyDetail($dealer_id)
    {
        if ($dealer_id < 1) {
            return false;
        }
        $condition['cd.dealer_id'] = ['eq', $dealer_id];
        //要查询的列表
        $cols = 'cd.dealer_id,cd.dealer_idcard,cd.sign_time,cd.auth_type,cd.auth_status,
                 cd.is_agree_agreement,cd.sign_contract_no,cd.is_three_in_one,cd.fullname,
                 cd.business_license_num,cd.organization_code,cd.tax_reg_cert_no,cd.contact_person,
                 cd.dealer_idcard,cd.business_license_pic,cd.organization_code_pic,cd.tax_reg_cert_pic,
                 cd.idcard_front_pic,cd.idcard_back_pic,cd.idcard_hold_pic,cd.auth_letter_pic,du.uid';
        $res = $this->CarDealer->alias('cd')->field($cols)
            ->join(C('DB_PREFIX') . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->where($condition)->find();
        $res['business_license_pic'] = get_content_cdnurl().$res['business_license_pic'];
        if(!empty($res['organization_code_pic'])){
            $res['organization_code_pic'] = get_content_cdnurl().$res['organization_code_pic'];
        }
        if(!empty($res['tax_reg_cert_pic'])){
            $res['tax_reg_cert_pic'] = get_content_cdnurl().$res['tax_reg_cert_pic'];
        }
        $res['idcard_front_pic'] = get_content_cdnurl().$res['idcard_front_pic'];
        $res['idcard_back_pic'] = get_content_cdnurl().$res['idcard_back_pic'];
        $res['idcard_hold_pic'] = get_content_cdnurl().$res['idcard_hold_pic'];
        $res['auth_letter_pic'] = get_content_cdnurl().$res['auth_letter_pic'];
        if (!empty($res)) {
            $dealer = new Dealer();
            $res = $dealer->get_contact_mobile_byAntNestfind($res);
        }
        $res['is_three_in_one'] = $res['is_three_in_one'] == 1 ? '是' : '否';
        return $res;
    }
}
